import { defineStore } from 'pinia'

export const useUserStore = defineStore('user', {
  state: () => ({
    name: '游客',
    age: 0,
    token: '',

    userId: null, // 用户ID
  }),
  getters: {
    fullName: (state) => `用户：${state.name}`,
    getUserId: (state) => {
      return state.userId
    }
  },
  actions: {
    login(username, password) {
      // 模拟登录
      this.name = username
      this.token = 'mock_token_' + Date.now()
      uni.showToast({
        title: '登录成功',
        icon: 'success'
      })
    },
    logout() {
      uni.showToast({
        title: '已退出登录',
        icon: 'none'
      })
    },
    updateUserId(userId) {
      this.userId = userId;
    }
  },
  persist: {
    enabled: true,
    strategies: [
      {
        key: 'pinia-user',
        storage: {
          getItem: (key) => uni.getStorageSync(key),
          setItem: (key, value) => uni.setStorageSync(key, value),
          removeItem: (key) => uni.removeStorageSync(key)
        },
        paths: ['userId']
      }
    ]
  }
})  